/*
 * Copyright (c) 2000-2017 wanglei. All rights reserved.
 * TeamDev PROPRIETARY and CONFIDENTIAL.
 * Use is subject to license terms.
 */

package com.xhb.ui;


import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * @author wanglei.
 */
public class AboutDialog extends JDialog {
    public AboutDialog(Frame owner) {
        super(owner, "关于", true);
        initContent();
        initKeyStroke();
        setResizable(false);
        pack();
        setLocationRelativeTo(owner);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }

    private void initContent() {
        JTextPane aboutText = new JTextPane();
        aboutText.setContentType("text/html");
        aboutText.setText("浏览器");
        aboutText.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        aboutText.setEditable(false);
        aboutText.addHyperlinkListener(new HyperlinkListener() {
            @SuppressWarnings("ParameterNameDiffersFromOverriddenParameter")
            @Override
            public void hyperlinkUpdate(HyperlinkEvent event) {
                if (event.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
                    try {
                        Desktop desktop = java.awt.Desktop.getDesktop();
                        desktop.browse(event.getURL().toURI());
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        add(aboutText, BorderLayout.CENTER);
    }

    private void initKeyStroke() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    dispose();
                }
            }
        });
        JRootPane rootPane = getRootPane();
        KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, "ESCAPE");
        rootPane.getActionMap().put("ESCAPE", new AbstractAction() {
            private static final long serialVersionUID = 6693635607417495802L;

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }
}
